'''
Author: @xiaonancheng
Date: 2023-02-27 14:42:50
https://www.cnblogs.com/xiaonancheng
Copyright (c) 2023 by @xiaonancheng, All Rights Reserved. 
'''
from django.shortcuts import render,redirect
from UserApp.models import User
from adminApp.models import Manager
from DoctorApp.models import Doctor
def test(request):
    return render(request,"user_")
def home(request):
    status = request.session.get('is_login')
    if not status:
        return redirect('/login/')
    return render(request,"home.html",{})
def login(request):
    message={"info":''}
    if request.method=="POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        print(username,password)
        if not username or not password:
            message["info"]="用户名或密码不能为空"
        else:
            # message["info"]="sussess"
            user_obj1 = User.objects.filter(user_name=username).first() 
            user_obj2 = Manager.objects.filter(manager_name=username).first()
            user_obj3 = Doctor.objects.filter(doctor_name=username).first()
            user_obj,k=(user_obj1,"/user") if user_obj1 else (user_obj2,"/admin_sys") if user_obj2 else ( user_obj3,"/doctor")
            if not user_obj:
                message["info"]="用户不存在"
            elif user_obj.states==0:
                message["info"]="您已被封号,或者还没同意您的账户请求，请联系管理员"
            elif user_obj.states==2:
                message["info"]="您的账户请求未通过，请重新注册"
            else:
                if password==user_obj.password:
                    request.session['is_login'] = True
                    request.session['username'] = username
                    request.session['uid'] = user_obj.uid
                    request.session['user_type'] = k
                    if k=="/doctor":
                        request.session['profile']=user_obj.profile
                    
                    return redirect(k+"/index/")
                else:
                    message["info"]="密码错误"


    
    return render(request,"login.html",message)
def logout(request):
    if not request.session.get('is_login', None):
        # 如果本来就未登录，也就没有登出一说
        return redirect("/login/")
    request.session.flush()
    # 或者使用下面的方法
    # del request.session['is_login']
    # del request.session['user_id']
    # del request.session['user_name']
    return redirect("/login/")
def register(request):
    message={"info":''}
    if request.method=="POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        email= request.POST.get("email")
        role=request.POST.get("role")
        sex=request.POST.get('sex')
        age=request.POST.get('age')
        if role=="1":
            user=User()
            user.user_name=username
            user.password=password
            user.email=email
            user.user_sex=sex
            user.user_age=age
            user.save()
            return redirect("/login/")
        elif role=="2":
            doctor=Doctor()
            doctor.doctor_name=username
            doctor.password=password
            doctor.email=email
            doctor.doctor_sex=sex
            doctor.doctor_age=age
            doctor.save()

            return redirect("/login/")
        else:
            message={"info":'角色错误'}

    # print( request.POST)
    return render(request,"register.html",message)
def error(request):#错误页面
    return render(request,"error.html")